<?php
	include_once 'autoload.php';
	

	session_start();

	$ctl = new JobselectCtl();
	$ctl->checkLogged();
	$ctl->checkNotReadonly();
	$ctl->proceed();
	
	$header = new Header();
	$header->render('Job Selection');
	
	// getting the form and proceed
	$form = $ctl->getJobselectForm();
	
	$albums = $form->getAlbums();
	$albumId = $form->getAlbumId();
	$jobId = $form->getJobId();
	$jobName = $form->getJobName();
	
	$combo = HtmlUtil::getComboBox('albumId', $albums, 'getId', 'getName', $albumId);
	
	$pictures = $form->getPictures();
	$jobPictures = $form->getJobPictures();
	
	// get the selected elements and ensure they will be held in some array
	$selected = $form->getSelectedPictures();
	$selected = is_array($selected) ? $selected : array();
	
	$seljob = $form->getSelectedJobPictures	();
	$seljob = is_array($seljob) ? $seljob : array();
?>
<form method="post">
<input type="hidden" name="pagename" value="jobselect" id="pgid">
<input type="hidden" name="metname" value="" id="metid">
<input type="hidden" name="jobId" value="<?php echo $jobId; ?>">
<input type="hidden" name="jobName" value="<?php echo $jobName; ?>">

<h2>Select Job Pictures</h2>
<h3>Job: <?php echo htmlspecialchars($jobName); ?></h3>
<table cellspacing="0" cellpadding="4">
	<tr>
		<td>Album: <?php echo $combo; ?> <input type="button" value="Update" onclick="triggerform('update');"></td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td valign="top">
		<table cellpadding="8" cellspacing="0" class="pictures" id="album">
			<thead>
			<tr>
				<td colspan="2" class="label">Album Photos</td>
			</tr>
			<tr>
				<td class="label"><input type="checkbox" onclick="tsl(event)"></td><td class="label"><input type="button" value="Select" onclick="triggerform('select')"></td>
			</tr>
			</thead>
			<tbody>
			<?php 
			foreach($pictures as $current) {
				$sel = array_key_exists($current, $selected);
				$check = HtmlUtil::getCheckbox('selectedPictures[]', $current, $sel, array('onclick' => 'tslb(event)'));
			?>
			<tr>
				<td class="label"><?php echo $check; ?></td>
				<td class="label"><img src="smallimage.php?id=<?php echo $current; ?>"></td>
			</tr>
			<?php 
			}
			?>
			</tbody>
		</table>
		</td>
		<td valign="top">
		<table cellpadding="8" cellspacing="0" class="pictures" id="selected">
			<thead>
			<tr>
				<td colspan="2" class="label">Selected Job Photos</td>
			</tr>
			<tr>
				<td class="label"><input type="checkbox" onclick="tsl(event)"></td><td class="label"><input type="button" value="Remove" onclick="doConfirm('Do you want to remove the selected pictures from the job?', 'remove')"></td>
			</tr>
			</thead>
			<tbody>
			<?php
			foreach($jobPictures as $current) { 
				$sel = array_key_exists($current, $seljob);
				$check = HtmlUtil::getCheckbox('selectedJobPictures[]', $current, $sel, array('onclick' => 'tslb(event)'));
			?>
			<tr>
				<td class="label"><?php echo $check; ?></td>
				<td class="label"><img src="smallimage.php?id=<?php echo $current; ?>"></td>
			</tr>
			<?php
			} 
			?>
			</tbody>
		</table>
		</td>
	</tr>
</table>
</form>
<?php
	$footer = new Footer();
	$footer->render();
?>
<script>
	redocheckboxes('table#album');
	redocheckboxes('table#selected');
</script>
